package cn.cxion.chat.app.adapter;

import android.content.Context;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;

import java.util.List;

import butterknife.Bind;
import cn.cxion.chat.R;
import cn.cxion.chat.app.activity.ConstantActivity;
import cn.cxion.chat.app.bean.CheckContractBean;
import cn.cxion.chat.app.bean.CheckProcessDetail;
import cn.cxion.chat.app.constant.Constant;
import cn.cxion.chat.app.util.ChronometerUtil;
import cn.cxion.chat.app.util.LocalDisplay;
import cn.cxion.chat.app.util.LogUtil;
import cn.cxion.chat.app.util.StringUtil;
import cn.cxion.chat.app.view.ApprovalProcessItemView;

public class ApprovalProcessAdapter extends RecyclerView.Adapter<ApprovalProcessViewHolder> {

    Context context;
    ApprovalProcessItemView approvalProcessItemView;
    List<CheckProcessDetail> list;
    CheckContractBean checkContractBean;
    int classType;

    public ApprovalProcessAdapter(Context context) {
        this.context = context;
    }

    public ApprovalProcessAdapter(Context context, int classType, CheckContractBean checkContractBean) {
        this.context = context;
        this.checkContractBean = checkContractBean;
        this.list = checkContractBean.getCheckProcessDetailList();
        this.classType = classType;
    }

    public CheckContractBean getCheckContractBean() {
        return checkContractBean;
    }

    public void setCheckContractBean(CheckContractBean checkContractBean) {
        this.checkContractBean = checkContractBean;
        this.list = checkContractBean.getCheckProcessDetailList();
    }

    public List<CheckProcessDetail> getList() {
        return list;
    }

    public void setList(List<CheckProcessDetail> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public ApprovalProcessViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        approvalProcessItemView = new ApprovalProcessItemView(context);
        return new ApprovalProcessViewHolder(approvalProcessItemView.getView());
    }

    @Override
    public void onBindViewHolder(@NonNull ApprovalProcessViewHolder holder, int position) {
        holder.llOnhand.setVisibility(View.GONE);
        holder.llCheckcontent.setVisibility(View.GONE);
        holder.llProcess.setVisibility(View.VISIBLE);
        holder.divide.setVisibility(View.GONE);
        if (position == 0) {
            holder.tvTopLine.setVisibility(View.INVISIBLE);
        }
        if (position > getItemCount() - 2) {
            holder.tvLine.setVisibility(View.INVISIBLE);
        } else {
            holder.tvLine.setVisibility(View.VISIBLE);
        }
        if (list == null || position >= list.size()) {
            holder.tvDot.setBackgroundResource(R.drawable.circle_gray_bg);
            holder.tvDot.setText("!");
            holder.imgState2.setVisibility(View.GONE);
            holder.imgFileexamine.setVisibility(View.GONE);
            if (position == 1) {
                holder.processTv1.setText("等待认领");
            } else if (position == 2) {
                holder.processTv1.setText("等待审改");
                holder.processTv2.setVisibility(View.GONE);
                holder.chronometer.setBase(checkContractBean.getCheckProcessDetailList().get(1).getTimestamp());
                ChronometerUtil.setFormat(holder.chronometer);
                holder.chronometer.start();
                holder.chronometer.setVisibility(View.VISIBLE);
            } else if (position == 3) {
                holder.processTv1.setText("等待内部审批");
                holder.processTv2.setVisibility(View.GONE);
            }else if (position == 4) {
                holder.processTv1.setText("等待客户确认");
                holder.processTv2.setVisibility(View.GONE);
            }
        } else {
            holder.imgState2.setVisibility(View.VISIBLE);
            holder.processTv2.setVisibility(View.VISIBLE);
            holder.imgFileexamine.setVisibility(View.VISIBLE);
            holder.chronometer.setVisibility(View.GONE);
            CheckProcessDetail detail = list.get(position);
            holder.tvDot.setBackgroundResource(R.drawable.circle_red_bg);
            holder.tvDot.setText(detail.getStateName());
            holder.imgState2.setImageResource(R.drawable.vector_onhand_itemtype3);
            holder.processTv1.setText(detail.getTitle());
            holder.processTv2.setText(detail.getTime());
            holder.processTv3.setText(detail.getOperator());
            holder.processTv4.setText("查看");
        }
        holder.llProcess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (position >= list.size()) {
                    if (position == 1) {
                        ConstantActivity.show(context, Constant.CLAIM, checkContractBean);
                    } else if (position == 2) {
                        ConstantActivity.show(context, Constant.CONTRACTDETAILS, checkContractBean);
                    } else if (position == 3)
                        ConstantActivity.show(context, Constant.APPROVE, checkContractBean);
                    else if (position == 4)
                        ConstantActivity.show(context, Constant.CUSTOMERAPPROVE, checkContractBean);
                } else {
                    if (position == 0)
                        ConstantActivity.show(context, Constant.DEMANDDETAILS, checkContractBean);
                    else if (position == 1)
                        ConstantActivity.show(context, Constant.CLAIMDETAIL, checkContractBean);
                    else if (position == 2)
                        ConstantActivity.show(context, Constant.CONTRACTDETAILS, checkContractBean);
                    else if (position == 3)
                        ConstantActivity.show(context, Constant.APPROVE, checkContractBean);
                    else if (position == 4)
                        ConstantActivity.show(context, Constant.CUSTOMERAPPROVE, checkContractBean);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (list == null)
            return 0;
        if (classType == Constant.CLAIM) {
            if (list.size() == 1) {
                return list.size() + 1;
            }
        }
        if (classType == Constant.CHECKCONTRACT) {
            if (list.size() >= 2&&checkContractBean.getCustomerApproveDetail()==null) {
                return list.size() + 1;
            }
        }
        return list.size();
    }
}
